package com.weisy.signin.ui.fragment;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.hjq.toast.ToastUtils;
import com.weisy.signin.R;
import com.weisy.signin.base.BaseFragment;
import com.weisy.signin.bean.MsgEvent;
import com.weisy.signin.bean.Student;
import com.weisy.signin.bean.Teacher;
import com.weisy.signin.comm.Comm;
import com.weisy.signin.network.WebConfig;
import com.weisy.signin.network.client.HttpClient;
import com.weisy.signin.utils.DeviceUtils;
import com.weisy.signin.utils.GsonUtils;
import com.weisy.signin.utils.Logs;
import com.weisy.signin.utils.SpUtils;
import com.weisy.signin.widget.LoadingDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author weisy
 * @function 成绩
 * @date 2019/3/21 21:22
 */
public class MyScoreFragment extends BaseFragment {
    @BindView(R.id.txt_score)
    TextView txtScore;
    @BindView(R.id.txt_mid_score)
    TextView txtMidScore;
    @BindView(R.id.txt_end_score)
    TextView txtEndScore;

    private WebConfig webConfig;
    private Student student;
    private HttpClient client;

    private LoadingDialog scoreDialog;

    private boolean isGet = false;

    @Override
    public int getLayoutId() {
        return R.layout.frag_score;
    }

    @Override
    public void initViews(View view) {
        EventBus.getDefault().register(this);
        if (SpUtils.contains(context, Comm.SP_PERION)) {
            String persion = (String) SpUtils.get(context, Comm.SP_PERION, "");
            student = GsonUtils.str2Bean(persion, Student.class);
        }
        scoreDialog = new LoadingDialog.Builder(context)
                .setMsg("获取中...")
                .create();
    }

    @Override
    public void initDatas() {
        if (null == student) return;
        if (0f != student.getScore()) {
            txtScore.setText(String.format(Locale.CHINESE, "%.1f", student.getScore()));
        }
        if (0f != student.getMidScore()) {
            txtMidScore.setText(String.format(Locale.CHINESE, "%.1f", student.getMidScore()));
        }
        if (0f != student.getEndScore()) {
            txtEndScore.setText(String.format(Locale.CHINESE, "%.1f", student.getEndScore()));
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMsgEvent(MsgEvent msgEvent) {
        if (null != scoreDialog && scoreDialog.isShowing()) {
            scoreDialog.dismiss();
        }
        switch (msgEvent.getCode()) {
            case Comm.SIGN_WEBCONFIG:
                webConfig = (WebConfig) msgEvent.getData();
                if (null != webConfig && null != student && !isGet) {
                    isGet = true;
                    client = new HttpClient(webConfig);
                    student.setDevice(DeviceUtils.getUniqueId(context));
                    client.send("/score", GsonUtils.bean2Str(student));
                }
                break;
            case Comm.SIGN_SCORE:
                if (null != scoreDialog && scoreDialog.isShowing()) {
                    scoreDialog.dismiss();
                }
                if (TextUtils.isEmpty(msgEvent.getMsg())) {
                    Student tmp = GsonUtils.str2Bean(msgEvent.getData().toString(), Student.class);
                    student.setScore(tmp.getScore());
                    student.setMidScore(tmp.getMidScore());
                    student.setEndScore(tmp.getEndScore());
                    Logs.d(TAG, "收到成绩:" + student.toString());
                    SpUtils.put(context, Comm.SP_PERION, GsonUtils.bean2Str(student));
                    initDatas();
                }
                break;
        }
    }

    @OnClick({R.id.btn_get_score})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_get_score:
                if (null != webConfig && null != student) {
                    if (null != scoreDialog && !scoreDialog.isShowing()) {
                        scoreDialog.show();
                    }
                    if (null == client) {
                        client = new HttpClient(webConfig);
                    }
                    student.setDevice(DeviceUtils.getUniqueId(context));
                    client.send("/score", GsonUtils.bean2Str(student));
                } else {
                    ToastUtils.show("请在签到过程中获取成绩");
                }
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
